package logic

import (
	"context"
	"errors"
	"user-item/models/mysql"

	"user-item/userrpc/internal/svc"
	"user-item/userrpc/userrpc"

	errors2 "github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatePerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatePerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePerfectLogic {
	return &UpdatePerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 完善用户信息
func (l *UpdatePerfectLogic) UpdatePerfect(in *userrpc.UpdatePerfectRequest) (*userrpc.UpdatePerfectResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	if err := user.FindUserByUserId(mysql.DB, in.UserId); err != nil {
		return nil, errors2.New("用户不存在")
	}
	user.HandImage = in.HandImage
	user.Age = in.Age
	user.Address = in.Address
	user.Email = in.Email
	err := user.UserUpdate(mysql.DB)
	if err != nil {
		return nil, errors.New("完善用户信息失败")
	}

	return &userrpc.UpdatePerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
